package com.gxa.community.admin.controller.order;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gxa.community.admin.service.order.AfterSalesService;
import com.gxa.community.core.pojo.bo.order.AfterSalesNoAndReason;
import com.gxa.community.core.pojo.vo.order.AfterSalesVo;
import com.gxa.community.core.utils.LittleUtils;
import com.gxa.community.core.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class AfterSalesController {

    final AfterSalesService afterSalesService;

    public AfterSalesController(AfterSalesService afterSalesService) {
        this.afterSalesService = afterSalesService;
    }

    /**
     * 查询售后信息
     *
     * @param a 查询条件
     */
    @PostMapping("/queryAfterSales")
    public R queryAfterSales(Integer page, Integer size, @RequestBody AfterSalesVo a) {
        // 数据校验
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1 || size > 100) size = 5;

        if (LittleUtils.checkNum(a.getPointType(), 1, 6)) return R.error("自提点类型有误");
        if (LittleUtils.checkNum(a.getAsType(), 1, 3)) return R.error("售后类型有误");
        if (LittleUtils.checkNum(a.getStatus(), 0, 3)) return R.error("售后状态有误");

        return afterSalesService.queryAfterSales(page, size, a);
    }

    /**
     * 同意售后申请并退款
     *
     * @param nos 售后编号
     */
    @PutMapping("/agreeAfterSales")
    public R agreeAfterSales(@RequestBody List<String> nos) {
        return afterSalesService.agreeAfterSales(nos);
    }

    /**
     * 拒绝售后申请
     *
     * @param nos 售后编号及拒绝理由
     */
    @PutMapping("/rejectAfterSales")
    public R rejectAfterSales(@RequestBody AfterSalesNoAndReason nos) {
        if (StringUtils.isBlank(nos.getRejectReason())) return R.error("拒绝理由不能为空");
        return afterSalesService.rejectAfterSales(nos);
    }

}
